package br.com.lopes.gci.web;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;

public class GCIPhaseListener implements PhaseListener {
	private static final long serialVersionUID = 15465654L;

	public void afterPhase(PhaseEvent event) {
		if (event.getFacesContext().getViewRoot() == null) {
			try {
				event.getFacesContext().getApplication().getNavigationHandler()
						.handleNavigation(event.getFacesContext(), null,
								"viewExpired");
			} catch (NullPointerException e) {
				ExternalContext externalContext = event.getFacesContext().getExternalContext();
				HttpServletRequest request =  (HttpServletRequest) externalContext.getRequest();
				try {
					externalContext.redirect(request.getContextPath() + "/index.jsp");
				} catch (IOException ioException) {

				}
			}
		}
	}

	public void beforePhase(PhaseEvent event) {

	}

	public PhaseId getPhaseId() {
		return PhaseId.RESTORE_VIEW;
	}
}